package top.leyi.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.leyi.IService.IAuthService;
import top.leyi.IService.IUserService;
import top.leyi.entity.user.User;
import top.leyi.exception.ClassNotSetException;
import top.leyi.exception.NotSuchUserIdException;
import top.leyi.exception.TokenException;
import top.leyi.mapper.UserMapper;

@Service
public class UserServiceImpl implements IUserService{

    @Autowired
    UserMapper userMapper;
    @Autowired
    IAuthService authService;
    @Override
    public User getPersonInfo(String token) throws TokenException {

        User user = userMapper.queryUserInfo(token);
        if(user == null){
            throw new TokenException();
        }
        user.setAttention(false);
        return user;
  }

    /**
     * 查询其他人信息
     * @param token
     * @param user
     * @return
     * @throws TokenException
     */
    @Override
    public User getOtherPersonInfo(String token, User user) throws NotSuchUserIdException,TokenException {
        User user1 = userMapper.queryOneUser(user.getId());

        if(user1 == null) {
            throw new  NotSuchUserIdException();
        }
        User fans = getPersonInfo(token);
        user1.setAttention(userMapper.queryIfAtten( user.getId(),fans.getId()) > 0);
        return user1;
    }


    /*
      当更新个人信息的时候
     */
    @Override
    public User updataPersonInfo( String token, User user )throws  TokenException, ClassNotSetException  {


        User older = getPersonInfo(token);

        if(user == null){
            throw new  TokenException();
        }
        /**
         * 将roleid 设置为3，也就是普通学生
         */
//        older.setRoleId(3);

        if(user.getRoleId() != 0){
            older.setRoleId(user.getRoleId());
        }
        if(user.getHeadPic() != null){
            older.setHeadPic(user.getHeadPic());
        }
        if(user.getUsername() != null){
            older.setUsername(user.getUsername());
        }
        if(user.getWechat() != null){
            older.setWechat(user.getWechat());
        }
        if(user.getQq() != null){
            older.setQq(user.getQq());
        }
        if(user.getPhone() != null){
            older.setPhone(user.getPhone());
        }
        if(user.getEmail() != null){
            older.setEmail(user.getEmail());
        }
        if(user.getWord() != null){
            older.setWord(user.getWord());
        }
        if(user.getBgPic() != null){
            older.setBgPic(user.getBgPic());
        }
        if(user.getCollege() != null){
            older.setCollege(user.getCollege());
        }
        if(user.getStudy_time() != null){
            older.setStudy_time(user.getStudy_time());
        }
        if(user.getClassId() != 0){
            older.setClassId(user.getClassId());
        }

       try{
           userMapper.insertUserDetils(older);
       }catch (Exception e){
          throw new ClassNotSetException();
       }

        authService.changeUserRole(older);

        older = getPersonInfo(token);
        return older;
    }

}
